// pages/modifierCompany/index.js
const baseUrl = require('../../config').baseUrl
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        companyName: '', //  单位名称
        companyId: '' // 单位id
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.companyId) {
            this.setData({
                companyId: options.companyId,
                companyName: options.companyName
            })
        } else {
            wx.setNavigationBarTitle({
                title: '添加检查单位'
            })
        }
    },
    changeCompanyName (e) {
        this.setData({
            companyName: e.detail.value
        })
    },
    modifiCompany () {
        if (this.data.companyName === '') {
            wx.showToast({
                title: '请输入单位名称',
                icon: 'none',
                duration: 1000
            })
            return false
        }
        wx.request({
            url: baseUrl,
            header: {
                "content-type": "application/x-www-form-urlencoded"
            },
            data: {
                data: JSON.stringify({
                    business: "SaveDeptInfo",
                    data: {
                        account: app.globalData.account,
                        deptname: this.data.companyName,
                        deptid: this.data.companyId
                    }
                })
            },
            method: 'POST',
            success (res) {
                if (res.data.code === 0) {
                    wx.setStorageSync('isReopen', true)
                    wx.navigateBack({
                        delta: 1
                    })
                } else {
                    wx.showToast({
                        title: res.data.info,
                        icon: 'none',
                        duration: 1000
                    })
                }
            },
            fail: (error) => {
                wx.showToast({
                    title: error.data.info,
                    icon: 'none',
                    duration: 1000
                })
            }
        })
    }
})